package com.heima.feign.clients.fallback;

import com.heima.feign.clients.AdminClient;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AdminClientFallbackFactory implements FallbackFactory<AdminClient> {
    @Override
    public AdminClient create(Throwable throwable) {

        log.info("即将执行 AdminClient 远程调用的服务降级策略..");

        return new AdminClient() {
            @Override
            public ResponseResult findAllSensitives() {
                log.error("执行远程调用时, AdminClient 的 findAllSensitive 执行异常失败, 失败原因: {}", throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult<AdChannel> findChannelById(Integer channelId) {
                log.error("文章发布业务-远程调用-查询所属频道信息失败，失败原因: {}, 请求参数: {}", throwable.getMessage(), channelId);
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }
        };
    }
}
